<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          size="mini"
          :disabled="single"
          @click="parity"
          >比价</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          size="mini"
          :disabled="multiple"
          @click="purchase"
          >生成采购订单</el-button
        >
      </el-col>
    </el-row>
    <el-table
      :data="orderList"
      @selection-change="handleSelectionChange"
      ref="dataTable"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="单据编号" align="center" prop="orderNumber" />
      <el-table-column
        label="上游单据编号"
        align="center"
        prop="parentOrderNumber"
      />
      <el-table-column label="单据类型" align="center" prop="typeStr" />
      <el-table-column label="单据状态" align="center" prop="status">
        <template slot-scope="scope">
          <div v-if="scope.row.status === '0'">未审核</div>
          <div v-if="scope.row.status === '1'">已审核</div>
        </template>
      </el-table-column>
      <el-table-column label="企业" align="center" prop="enterpriseName" />
      <el-table-column
        label="单据时间"
        align="center"
        prop="orderTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="物料编码" align="center" prop="code" />
      <el-table-column label="物料名称" align="center" prop="name" />
      <el-table-column label="数量" align="center" prop="num" />
      <el-table-column label="单价" align="center" prop="price" />
      <el-table-column label="承诺到货时间" align="center" prop="commitmentTime" />
      <el-table-column label="供应商" align="center" prop="supplierName" />
    </el-table>
  </div>
</template>

<script>
import { searchQuote, purchaseOrder } from "@/api/system/order";

export default {
  name: "BjOrderDetial",
  data() {
    return {
      id: null,
      checkedId: null,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: false,
      // 非多个禁用
      multiple: true,
      // 单据表格数据
      orderList: [],
      total: null,
    };
  },
  created() {
    this.id = this.$route.query.id;
    this.getList();
  },
  methods: {
    /** 查询单据列表 */
    getList() {
      let obj = {
        id: this.id,
      };
      searchQuote(obj).then((response) => {
        this.orderList = response.data;
        this.total = response.data.length;
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.checkedId = selection[0].id;
      this.single = selection.length >= 1;
      this.multiple = selection.length !== 1;
    },
    toggleSelection(rows) {
      rows.forEach((row) => {
        this.$refs.dataTable.toggleRowSelection(row);
      });
    },
    // 比价
    parity() {
      if (this.total == 1) {
        this.$refs.dataTable.toggleRowSelection(this.orderList[0]);
      } else if (this.total > 1) {
        let arr = [];
        this.orderList.forEach((element) => {
          arr.push(element.price);
        });
        let min = Math.min(...arr);
        this.orderList.forEach((element) => {
          if (min == element.price) {
            this.$refs.dataTable.toggleRowSelection(element);
          }
        });
      }
    },
    // 生成采购订单
    purchase() {
      let obj = {
        id: this.checkedId,
      };
      purchaseOrder(obj).then((response) => {
        this.$message({
          message: "操作成功",
          type: "success",
        });
      });
    },
  },
};
</script>
